package com.hanggold.ppt;

import com.aspose.slides.ISlide;
import com.aspose.slides.License;
import com.aspose.slides.Presentation;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @author <a href="mailto:sunmch@163.com">smc</a>
 * @date 2019-09-18 17:17
 * @since 1.0.0
 **/
public class PPT2ImageDemo {
    private static String PPT_FILE = "H:" + File.separator + "yjc.pptx";

    private static String IMAGE_DIR = "H:" + File.separator + "ppt2img" + File.separator;

    private static String PPT_FILE_PROP_NAME = "ppt.file";

    private static String PPT_TO_IMG_DIR = "ppt.image.dir";

    public static void main(String[] args) throws Exception {

        InputStream license = PPT2ImageDemo.class.getClassLoader().getResourceAsStream("license.xml");
        if (license != null) {
            License aposeLic = new License();
            aposeLic.setLicense(license);
        }

        PPT_FILE = System.getProperty(PPT_FILE_PROP_NAME, PPT_FILE);

        IMAGE_DIR = System.getProperty(PPT_TO_IMG_DIR, IMAGE_DIR);

        System.out.printf("ppt文件名称: %s \n", PPT_FILE);

        System.out.printf("ppt生成的图片目录: %s \n", IMAGE_DIR);

        long start = System.currentTimeMillis();

        FileInputStream fileInputStream = new FileInputStream(new File(PPT_FILE));

        Presentation pres = new Presentation(fileInputStream);

        int i;
        for (i = 0; i < pres.getSlides().size(); i++) {
            ISlide slide = pres.getSlides().get_Item(i);
            int height = (int) (pres.getSlideSize().getSize().getHeight());
            int width = (int) (pres.getSlideSize().getSize().getWidth());
            BufferedImage image = slide.getThumbnail(new java.awt.Dimension(width, height));
            //每一页输出一张图片
            File outImage = new File(IMAGE_DIR + File.separator + (i + 1) + ".JPG");
            ImageIO.write(image, "JPG", outImage);
        }

        long end = System.currentTimeMillis();

        System.out.printf("转换pdf花费时间 :  %d", (end - start));
    }





}
